﻿#include<QJsonObject>
#include<QJsonArray>
#include<QJsonDocument>
#include<QDateTime>
#include<QJsonArray>
#include<QJsonObject>
#include<QJsonDocument>
#include<iostream>
#include"datapre.h"


MemberManage::MemberManage(){}

MemberManage::~MemberManage(){}

bool MemberManage::initFromJsonString(const std::string &json)
{
    members.clear();
    if (json==""){
        return true;
    }else{
        QJsonDocument jd = QJsonDocument::fromJson(QString::fromStdString(json).toLocal8Bit());
        if (jd.isArray()){
            QJsonArray ja = jd.array();
            for(int i=0;i<ja.size();i++){
                QJsonObject jo = ja.at(i).toObject();

                bool enable;
                if (jo.value("enable").toString()=="T"){
                    enable = true;
                }else if (jo.value("enable").toString()=="F"){
                    enable = false;
                }else{
                    return false;
                }
                members.push_back(Member{jo.value("id").toString().toStdString(),
                                         jo.value("name").toString().toStdString()
                                         ,enable});
            }

        }
    }
    return true;
}


bool MemberManage::addMember(const std::string & id,const std::string &name){
    Member m{id,name,true};
    members.push_back(m);
}

bool MemberManage::editMember(const int &index, const std::string &id, const std::string &name){
    if(members.at(index).id==id){
        members.at(index).name = name;
        return true;
    }else{
        return false;
    }

}
std::string MemberManage::toJson(){
    QJsonArray jsonArr;
    for(int i=0;i<members.size();i++){
        QJsonObject l;
        l.insert("enable",QString::fromStdString(members.at(i).enable?"T":"F"));
        l.insert("name",QString::fromStdString(members.at(i).name));
        l.insert("id",QString::fromStdString(members.at(i).id));
        jsonArr.append(l);
    }
    QJsonDocument jd(jsonArr);
    QString s;
    s.prepend(jd.toJson());
    return s.toStdString();
}

std::string MemberManage::getNewId(){
    QDate d(1970,1,1);
    QTime t(0,0,0,0);
    // QString b=tr("%1").arg(QDateTime::msecsTo(QDateTime(d,t)));
    QDateTime dt = QDateTime::currentDateTime();
    return QString::number(-dt.msecsTo(QDateTime(d,t))).toStdString();
}

Member MemberManage::getMember(const int &i){
    return members.at(i);
}

int MemberManage::count(){
    return members.size();
}

bool MemberManage::deleteMember(){
    //TODO  implement it later
}


DayLeave::DayLeave()
{
}



bool DayLeave::addLeave(const OneLeave oneleave)
{
    //TODO check ever exist
    this->leaves.push_back(oneleave);
    return true;

}

std::string DayLeave::toJson(){
    QJsonObject jsonObj;
    jsonObj.insert("date",QString::fromStdString(this->date));
    QJsonArray jsonArr;
    for(int i=0;i<leaves.size();i++){
        QJsonObject l;
        l.insert("m_id",QString::fromStdString(leaves.at(i).m_id));
        l.insert("m_name",QString::fromStdString(leaves.at(i).m_name));
        l.insert("reason",QString::fromStdString(leaves.at(i).reason));
        l.insert("time",QString::number(leaves.at(i).time));
        jsonArr.append(l);
    }
    jsonObj.insert("leaves",jsonArr);
    QJsonDocument jd(jsonObj);
    QString s;
    s.prepend(jd.toJson());
    return s.toStdString();
}

int DayLeave::count(){
    return this->leaves.size();
}

OneLeave DayLeave::getLeave(const int &index){
    return leaves.at(index);
}

bool DayLeave::initFromJsonString(const std::string & date,const std::string & json){

    this->date = date;
    this->leaves.clear();
    try {
        QJsonDocument jd = QJsonDocument::fromJson(QString::fromStdString(json).toLocal8Bit());
        QJsonObject jo = jd.object();

        QJsonValue jv = jo.value("date");
        if (jv.isString()){
            if(jv.toString().toStdString()!=this->date){
                return false;
            }
        }
        QJsonValue jva = jo.value("leaves");
        if(jva.isArray()){
            QJsonArray ja = jva.toArray();
            for(int i=0;i<ja.size();i++){
                QJsonObject jo = ja.at(i).toObject();
                leaves.push_back(OneLeave{jo.value("m_id").toString().toStdString(),
                                          jo.value("m_name").toString().toStdString(),
                                          jo.value("reason").toString().toStdString(),
                                          jo.value("time").toString().toInt()});
            }
        }
        return true;
    } catch (QJsonParseError e) {
        return false;
    }
}

bool DayLeave::removeLeave(const int &index){
    leaves.erase(leaves.begin()+index);
}

std::string DayLeave::getDateStr(){
    return date;
}
